<?php

//	ThinMVC v.1
//	http://code.google.com/p/phpthinmvc/
//
//	Copyright 2009 Site Solutions, Inc.
//
//	Licensed under the Apache License, Version 2.0 (the "License");
//	you may not use this file except in compliance with the License.
//	You may obtain a copy of the License at
//
//		 http://www.apache.org/licenses/LICENSE-2.0
//
//	Unless required by applicable law or agreed to in writing, software
//	distributed under the License is distributed on an "AS IS" BASIS,
//	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//	See the License for the specific language governing permissions and
//	limitations under the License.

class ThinMVC_Controller
{
	function ThinMVC_Controller ( &$caller, $object='', $next_steps )
	{
		$this->mvc=&$caller;
		$this->object=$object;
		$this->dbh=&$caller->dbh;
		$this->config=&$caller->config;
		if ($next_steps) {
			array_shift($next_steps);
			$this->path=$next_steps;
		} else {
			$this->path=array();
		}

		// Figure out the url path
		$this->urlpath=$caller->config['appurl'].$caller->info['atPath'];

		// Initialize (if needed)
		$this->_init();

	}

	// Default empty _init
	function _init() {
	}

	//////////
	// Return the 'view' file for the current request, ready for include.
	// Pass a name (like 'jerry.php') to load a particular file, or
	// leave it blank for it to load the default file for your request.
	// If no view is found it will try to call default_method (unless
	// 'no_default' is set). 
	// If all else fails an error is displayed to the end user.
	//////////

	function view( $view_file = '', $silent=0, $no_default=0 )
	{

		// Default to view the same name as the function
		if (!($view_file)) {
			if ($this->mvc->info['subPath']) {
				$view_file=$this->mvc->info['subPath'];
			} else {

				if (method_exists($this,"default_method") && !($no_default)) {
					$this->default_method();
					return;
				}

				if (!($silent)) {
					$this->mvc->error("No view requested.");
				}
				return;
			}
		}

		if (!(strpos($view_file,'.'))) {
			$view_file.=".php";
		}

		// Check the paths to see which view files exist--fall back to 
		// paths as specified
		foreach ($this->config['viewchain'] as $view_path) {
			if (file_exists( $this->mvc->info['thisPath']."/".$view_path."/".$view_file)) {
				return $this->mvc->info['thisPath']."/".$view_path."/".$view_file;
			}
		}

		if (method_exists($this,"default_method") && !($no_default)) {
			$this->default_method();
			return;
		}

		$not_found=substr( $this->mvc->info['thisPath'], strlen( $this->config['root'] ) );

		if (!($silent)) {
			$this->mvc->error("No view found: ".$not_found."/(view)/".$view_file);
		}
	}

	//////////
	// Call the next routine down the line.
	//////////

	function callNext()
	{
		$this->mvc->callNext();
	}

	//////////
	// Execute a redirect cleanly & log it.
	//////////

	function redirect( $url )
	{
		$content="Redirect: ".$url;
		$this->mvc->log_hit_end( $content );

		ob_clean(); // Throw out any output we have so far...
		header("Location: $url");
		exit;
	}
}
?>
